home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / Goodies / NEWINT~1 / TRACBA~1 / TRACBA~1.CLS < prev    next >
Text File  |  1997-06-04  |  12KB  |  374 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "CTracBar32"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10.  
  11. Option Explicit
  12. Private Const WM_VSCROLL = &H115
  13. Private Const WM_HSCROLL = &H114
  14.  
  15. Dim TracBarWnd As Long
  16. Private Const WM_COMMAND = &H111
  17. Private Const WM_COMMNOTIFY = &H44
  18.  
  19. Private Type tagInitCommonControlsEx
  20.     lngSize As Long
  21.     lngICC As Long
  22. End Type
  23. Const ICC_BAR_CLASSES = &H20
  24. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  25. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
  26. Private Declare Function InitCommonControlsEx Lib "Comctl32.dll" _
  27.  (iccex As tagInitCommonControlsEx) As Boolean
  28. Private Type RECT
  29.         left As Long
  30.         Top As Long
  31.         Right As Long
  32.         Bottom As Long
  33. End Type
  34. Private Const WHITE_BRUSH = 0
  35. Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
  36.  
  37. Private Type WNDCLASS
  38.     Style As Long
  39.     lpfnWndProc As Long
  40.     cbClsExtra As Long
  41.     cbWndExtra2 As Long
  42.     hInstance As Long
  43.     hIcon As Long
  44.     hCursor As Long
  45.     hbrBackground As Long
  46.     lpszMenuName As String
  47.     lpszClassName As String
  48. End Type
  49. Private Type WNDCLASSEX
  50.     cbSize As Long
  51.     Style As Long
  52.     lpfnWndProc As Long
  53.     cbClsExtra As Long
  54.     cbWndExtra As Long
  55.     hInstance As Long
  56.     hIcon As Long
  57.     hCursor As Long
  58.     hbrBackground As Long
  59.     lpszMenuName As String
  60.     lpszClassName As String
  61.     hIconSm As Long
  62. End Type
  63.  
  64. Private Declare Function RegisterClass Lib "user32" (Class As WNDCLASS) As Long
  65. Private Declare Function RegisterClassEx Lib "user32" Alias "RegisterClassExA" (pcWndClassEx As WNDCLASSEX) As Integer
  66.  
  67. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
  68.  
  69.  
  70.  
  71. Private Declare Function InvalidateRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT, ByVal bErase As Long) As Long
  72. Private Declare Function UpdateWindow Lib "user32" (ByVal hwnd As Long) As Long
  73. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
  74. 'Private Const TB_ADDSTRINGA = (WM_USER + 28)
  75. Private Declare Function LoadBitmap Lib "user32" Alias "LoadBitmapA" (ByVal hInstance As Long, ByVal lpBitmapName As String) As Long
  76. 'Public Const DI_MASK = 1 'VBC NR
  77. 'Public Const DI_IMAGE = 2 'VBC NR
  78. Private Const DI_NORMAL = 3
  79. 'Public Const DI_COMPAT = 4 'VBC NR
  80.  
  81. Const HWND_TOPMOST = -1
  82. Const SW_HIDE = 0
  83. Const SW_SHOWNORMAL = 1
  84.  
  85. Const SWP_NOSIZE = &H1
  86. Const SWP_NOMOVE = &H2
  87. Const SWP_NOREDRAW = &H8
  88. Const SWP_SHOWWINDOW = &H40
  89.  
  90.  
  91. Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
  92. Private Const COLOR_BTNFACE = 15
  93. Private Const COLOR_BTNTEXT = 18
  94. ' Window Style constants
  95. Const WS_VISIBLE = &H10000000
  96. Const WS_CHILD = &H40000000
  97. Const WS_POPUP = &H80000000
  98.  
  99. ' CreateWindow constants
  100. Const CW_USEDEFAULT = &H80000000
  101.  
  102. Private Declare Function SendStringMessage Lib "user32" Alias "SendMessageA" _
  103.  (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
  104. Private Declare Function CreateWindowEX Lib "user32" Alias "CreateWindowExA" _
  105.  (ByVal dwExStyle As Long, _
  106.  ByVal lpClassName As String, ByVal lpWindowName As String, _
  107.  ByVal dwStyle As Long, ByVal X As Long, ByVal Y As Long, _
  108.  ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, _
  109.  ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
  110. Private Declare Function DestroyWindow Lib "user32" _
  111.  (ByVal hwnd As Long) As Long
  112.  
  113. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
  114.  (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  115. Private Declare Function ShowWindow Lib "user32" _
  116.  (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
  117.  
  118. Private Declare Function MoveWindow Lib "user32" _
  119.  (ByVal hwnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, _
  120.  ByVal nHeight As Long, ByVal bRepaint As Long) As Long
  121. Private Declare Function IsWindowVisible Lib "user32" _
  122.  (ByVal hwnd As Long) As Long
  123.  
  124.  
  125.  
  126.  
  127.  
  128. Private Const WM_PAINT = &HF
  129.  
  130. Private Const WM_USER = &H400
  131. Private Const GWL_HWNDPARENT = (-8)
  132. Private Const GWL_STYLE = (-16)
  133.  
  134.  
  135.  
  136. Private Const WS_BORDER = &H800000
  137. Private Const WM_DRAWITEM = &H2B
  138. Private Const WS_CLIPCHILDREN = &H2000000
  139. Private Const WS_CLIPSIBLINGS = &H4000000
  140. Private Const WM_SETREDRAW = &HB
  141. '//Common Control Constants
  142. Private Const CCS_TOP = &H1
  143. Private Const CCS_NOMOVEY = &H2
  144. Private Const CCS_BOTTOM = &H3
  145. Private Const CCS_NORESIZE = &H4
  146. Private Const CCS_NOPARENTALIGN = &H8
  147. 'Private Const CCS_ADJUSTABLE          &H00020L
  148. Private Const CCS_NODIVIDER = &H40
  149. 'Private Const CCS_VERT                &H00080L
  150. 'Private Const CCS_LEFT                (CCS_VERT | CCS_TOP)
  151. 'Private Const CCS_RIGHT               (CCS_VERT | CCS_BOTTOM)
  152. 'Private Const CCS_NOMOVEX             (CCS_VERT | CCS_NOMOVEY)
  153.  
  154.  
  155. Private Declare Function SetWindowWord Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal wNewWord As Long) As Long
  156.  
  157.  
  158. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  159. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
  160. 'Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
  161. Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
  162.  
  163.   
  164.  
  165.  
  166.  
  167. '//
  168. 'Private Declare Function CreateToolbarEx Lib "COMCTL32" (ByVal hWnd As Long, ByVal ws As Long, ByVal wID As Long, ByVal nBitmaps As Long, ByVal hBMInst As Long, ByVal wBMID As Long, ByRef lpButtons As TBBUTTON, ByVal iNumButtons As Long, ByVal dxButton As Long, ByVal dyButton As Long, ByVal dxBitmap As Long, ByVal dyBitmap As Long, ByVal uStructSize As Long) As Long
  169.  
  170. Private Const PROGRESS_CLASSA = "msctls_progress32"
  171.  
  172.  
  173. Private Const TRACKBAR_CLASSA = "msctls_trackbar32"
  174.  
  175. Private Const TBS_AUTOTICKS = &H1
  176. Private Const TBS_VERT = &H2
  177. Private Const TBS_HORZ = &H0
  178. Private Const TBS_TOP = &H4
  179. Private Const TBS_BOTTOM = &H0
  180. Private Const TBS_LEFT = &H4
  181. Private Const TBS_RIGHT = &H0
  182. Private Const TBS_BOTH = &H8
  183. Private Const TBS_NOTICKS = &H10
  184. Private Const TBS_ENABLESELRANGE = &H20
  185. Private Const TBS_FIXEDLENGTH = &H40
  186. Private Const TBS_NOTHUMB = &H80
  187. Private Const TBS_TOOLTIPS = &H100
  188. '"TB_THUMPOSITION"
  189. Private Const TBM_GETPOS = (WM_USER)
  190. Private Const TBM_GETRANGEMIN = (WM_USER + 1)
  191. Private Const TBM_GETRANGEMAX = (WM_USER + 2)
  192. Private Const TBM_GETTIC = (WM_USER + 3)
  193. Private Const TBM_SETTIC = (WM_USER + 4)
  194. Private Const TBM_SETPOS = (WM_USER + 5)
  195. Private Const TBM_SETRANGE = (WM_USER + 6)
  196. Private Const TBM_SETRANGEMIN = (WM_USER + 7)
  197. Private Const TBM_SETRANGEMAX = (WM_USER + 8)
  198. Private Const TBM_CLEARTICS = (WM_USER + 9)
  199. Private Const TBM_SETSEL = (WM_USER + 10)
  200. Private Const TBM_SETSELSTART = (WM_USER + 11)
  201. Private Const TBM_SETSELEND = (WM_USER + 12)
  202. Private Const TBM_GETPTICS = (WM_USER + 14)
  203. Private Const TBM_GETTICPOS = (WM_USER + 15)
  204. Private Const TBM_GETNUMTICS = (WM_USER + 16)
  205. Private Const TBM_GETSELSTART = (WM_USER + 17)
  206. Private Const TBM_GETSELEND = (WM_USER + 18)
  207. Private Const TBM_CLEARSEL = (WM_USER + 19)
  208. Private Const TBM_SETTICFREQ = (WM_USER + 20)
  209. Private Const TBM_SETPAGESIZE = (WM_USER + 21)
  210. Private Const TBM_GETPAGESIZE = (WM_USER + 22)
  211. Private Const TBM_SETLINESIZE = (WM_USER + 23)
  212. Private Const TBM_GETLINESIZE = (WM_USER + 24)
  213. Private Const TBM_GETTHUMBRECT = (WM_USER + 25)
  214. Private Const TBM_GETCHANNELRECT = (WM_USER + 26)
  215. Private Const TBM_SETTHUMBLENGTH = (WM_USER + 27)
  216. Private Const TBM_GETTHUMBLENGTH = (WM_USER + 28)
  217. Private Const TBM_SETTOOLTIPS = (WM_USER + 29)
  218. Private Const TBM_GETTOOLTIPS = (WM_USER + 30)
  219. Private Const TBM_SETTIPSIDE = (WM_USER + 31)
  220. '// TrackBar Tip Side flags
  221. Private Const TBTS_TOP = 0
  222. Private Const TBTS_LEFT = 1
  223. Private Const TBTS_BOTTOM = 2
  224. Private Const TBTS_RIGHT = 3
  225.  
  226. Private Const TBM_SETBUDDY = (WM_USER + 32) ' // wparam = BOOL fLeft; (or right)
  227. Private Const TBM_GETBUDDY = (WM_USER + 33) ' // wparam = BOOL fLeft; (or right)
  228.  
  229.  
  230. Private Const TB_LINEUP = 0
  231. Private Const TB_LINEDOWN = 1
  232. Private Const TB_PAGEUP = 2
  233. Private Const TB_PAGEDOWN = 3
  234. Private Const TB_THUMBPOSITION = 4
  235. Private Const TB_THUMBTRACK = 5
  236. Private Const TB_TOP = 6
  237. Private Const TB_BOTTOM = 7
  238. Private Const TB_ENDTRACK = 8
  239.  
  240.  
  241. '// custom draw item specs
  242. Private Const TBCD_TICS = &H1
  243. Private Const TBCD_THUMB = &H2
  244. Private Const TBCD_CHANNEL = &H3
  245.  
  246. Dim mlngStyle As Long
  247. Dim mlngTop As Long
  248. Dim mlngLeft As Long
  249. Dim mlngWidth As Long
  250. Dim mlngHeight As Long
  251. Dim mfrmParent As Object
  252. Dim mstrFormat As String
  253. Dim mvarMin As Variant
  254. Dim mvarMax As Variant
  255.  
  256. Public Function GetTracBarHwnd()
  257. GetTracBarHwnd = TracBarWnd
  258. End Function
  259.  
  260.  
  261. Private Sub Class_Initialize()
  262.  Dim iccex As tagInitCommonControlsEx
  263.     With iccex
  264.         .lngSize = LenB(iccex)
  265.         .lngICC = ICC_BAR_CLASSES
  266.     End With
  267.     Call InitCommonControlsEx(iccex)
  268.  
  269.    TracBarWnd = 0
  270. End Sub
  271.  
  272.  
  273.  
  274. Public Function Create( _
  275.  Optional left As Variant, _
  276.  Optional Top As Variant, _
  277.  Optional Width As Variant, _
  278.  Optional Height As Variant, Optional Vertical As Boolean) _
  279.   As Boolean
  280. Dim VertLong As Long
  281. VertLong = 0
  282. If Parent Is Nothing Then
  283.       Create = False
  284.       Exit Function
  285. End If
  286.  If Vertical = True Then VertLong = TBS_VERT
  287.     If IsMissing(left) Then left = 0
  288.     If IsMissing(Top) Then Top = 0
  289.     If IsMissing(Width) Then Width = Parent.Width \ Screen.TwipsPerPixelX
  290.     If IsMissing(Height) Then Height = 25
  291.  
  292.  
  293.  
  294.     TracBarWnd = CreateWindowEX(0, "msctls_trackbar32", "", _
  295.           WS_CHILD Or WS_VISIBLE Or TBS_HORZ Or VertLong Or TBTS_LEFT Or TBS_RIGHT, 0, 0, 0, 0, _
  296.      Parent.hwnd, 0&, App.hInstance, 0&)
  297.  
  298.   Dim X As Integer
  299.  
  300.   Dim Range(1) As Integer
  301.   Range(0) = 0
  302.   Range(1) = 100
  303.     Create = (TracBarWnd <> 0)
  304.      Call SendMessage(TracBarWnd, TBM_SETTICFREQ, 10, 0)
  305.    
  306.   ' (Minimum range = low word, Maximum range = high word).
  307.  
  308.   
  309.      
  310.     Call SendMessage(TracBarWnd, TBM_SETRANGE, True, ByVal (&H100 * &H10000))
  311.   
  312.  ' // Set the initial range.
  313.    
  314.     Call SendMessage(TracBarWnd, TBM_SETRANGEMIN, True, ByVal 1)  'CLng(1))
  315.      Call SendMessage(TracBarWnd, TBM_SETRANGEMAX, True, ByVal 100)  'CLng(100))
  316.  
  317. '  {
  318.       Call SetParent(TracBarWnd, Parent.hwnd)
  319.  
  320.  
  321.       Call MoveWindow(TracBarWnd, CLng(left), CLng(Top), CLng(Width), CLng(Height), True)
  322.       Call ShowWindow(TracBarWnd, SW_SHOWNORMAL)
  323.    
  324. End Function
  325. Public Property Get Parent() As Object
  326.     Set Parent = mfrmParent
  327. End Property
  328.  
  329. Public Property Set Parent(frm As Object)
  330.     Set mfrmParent = frm
  331. End Property
  332.  
  333.  
  334. Private Sub Class_Terminate()
  335.  Exit Sub
  336.     If TracBarWnd <> 0 Then
  337.         Call DestroyWindow(TracBarWnd)
  338.     End If
  339. End Sub
  340.  
  341. Public Sub DestroyTracBar()
  342. On Error Resume Next
  343.  
  344. If TracBarWnd <> 0 Then
  345.  
  346.         Call DestroyWindow(TracBarWnd)
  347.     End If
  348. End Sub
  349.  
  350. Public Sub ClearTracBar()
  351. DoEvents
  352. Call SendMessage(TracBarWnd, TBM_SETPOS, 0, 0)
  353. DoEvents
  354. End Sub
  355.  
  356. Public Sub SetTracBarPos(TracPos As Integer)
  357. DoEvents
  358.  Call SendMessage(TracBarWnd, TBM_SETPOS, True, ByVal CLng(TracPos))
  359.  Call UpdateWindow(TracBarWnd)
  360.  
  361. DoEvents
  362. End Sub
  363.  
  364.  
  365.  
  366. Public Function GetTracBarPos()
  367. DoEvents
  368. 'Dim x As Integer
  369.  GetTracBarPos = SendMessage(TracBarWnd, TBM_GETPOS, 0, 0)
  370.  
  371.  
  372. DoEvents
  373. End Function
  374.